import uuid
from django.db import models
from django.contrib.auth.models import *
from ticketer import fields

# Create your models here.

def generate_uuid(self):
    uuid = uuid.uuid4()
    return uuid

class Ticket(models.Model):
    """
    Main ticket model.  This model holds on the pertinent information for tickets stored in the db.
    """

    subject = models.CharField(max_length=150)
    created_at = models.DateTimeField(auto_now=True)
    updated_at = models.DateTimeField(auto_now_add=True, auto_now=True)
    PRIORITY_CHOICES = (
        ('N', 'Normal'),
        ('U', 'Urgent'),
        ('E', 'Emergency'),
    )
    priority = models.CharField(max_length=1, choices=PRIORITY_CHOICES, default='N')
    STATUS_CHOICES = (
        ('O', 'Open'),
        ('N', 'New'),
        ('C', 'Closed'),
        ('S', 'Solved'),
    )
    status = models.CharField(max_length=2, choices=STATUS_CHOICES, default='N')
    owner = models.ForeignKey(User, default=None)
    tags = models.CharField(max_length=255)
    TICKET_TYPES = (
        ('E', 'Easy'),
        ('M', 'Medium'),
        ('H', 'Hard'),
        ('EP', 'Epic')
    )
    ttype = models.CharField(max_length=2, choices=TICKET_TYPES, default='M')
    uuid = models.CharField(max_length=64)
    

    def __unicode__(self):
        return self.subject

    def generate_uuid(self):
        uuid = uuid.uuid4()
        return uuid

class Comment(models.Model):
    """ 
    Ticket comment thread.  Linked to a ticket via foreign key
    """
    created_at = models.DateTimeField(auto_now=True)
    updated_at = models.DateTimeField(auto_now_add=True, auto_now=True)
    commenter = models.ForeignKey(User, default=None)
    thread = models.ForeignKey(Ticket, default=None) 
    comment = fields.PickledObjectField()
     
